package com.rede.didiok.community.service.impl;

import java.util.*;

import javax.annotation.Resource;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rede.didiok.common.constant.conf.MessageConf;
import com.rede.didiok.common.constant.conf.SQLConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.dto.community.UserMomentTopicDto;
import com.rede.didiok.common.enums.StatusEnum;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.utils.StringUtils;
import com.rede.didiok.common.utils.business.FileFeignUtil;
import com.rede.didiok.community.dao.UserMomentTopicDao;
import com.rede.didiok.community.entity.UserMomentTopicEntity;
import com.rede.didiok.community.service.UserMomentTopicService;


@Service("userMomentTopicService")
public class UserMomentTopicServiceImpl extends ServiceImpl<UserMomentTopicDao, UserMomentTopicEntity> implements UserMomentTopicService {

    @Resource
    UserMomentTopicService userMomentTopicService;
    @Resource
    FileFeignUtil fileFeignUtil;

    /***************************** admin:start **************************/
    // region  didiok-admin

    @Override
    public UserMomentTopicEntity getDetailByUid(UserMomentTopicDto userMomentTopicDto) {
        UserMomentTopicEntity topicEntity = this.getById(userMomentTopicDto.getUid());

        List<String> fileUidList = new ArrayList<>();
        if (StringUtils.isNotEmpty(topicEntity.getFileUid())) {
            fileUidList.add(topicEntity.getFileUid());
            Map<String, String> pictureMap = fileFeignUtil.fileUidToFileUrlMap(fileUidList);
            //获取图片
            if (StringUtils.isNotEmpty(topicEntity.getFileUid()) && pictureMap.get(topicEntity.getFileUid()) != null) {
                topicEntity.setPhotoUrl(pictureMap.get(topicEntity.getFileUid()));
            }
        }

        return topicEntity;
    }

    @Override
    public IPage<UserMomentTopicEntity> getPageList(UserMomentTopicDto userMomentTopicDto) {
        QueryWrapper<UserMomentTopicEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(userMomentTopicDto.getKeyword()) && !StringUtils.isEmpty(userMomentTopicDto.getKeyword().trim())) {
            queryWrapper.like(SQLConf.CONTENT, userMomentTopicDto.getKeyword().trim());
        }

        if (userMomentTopicDto.getIsPublish() != null) {
            queryWrapper.eq(SQLConf.IS_PUBLISH, userMomentTopicDto.getIsPublish());
        }

        Page<UserMomentTopicEntity> page = new Page<>();
        page.setCurrent(userMomentTopicDto.getCurrentPage());
        page.setSize(userMomentTopicDto.getPageSize());
        queryWrapper.eq(SQLConf.STATUS, StatusEnum.ENABLE);
        queryWrapper.orderByDesc(SQLConf.SORT);
        IPage<UserMomentTopicEntity> pageList = userMomentTopicService.page(page, queryWrapper);
        List<UserMomentTopicEntity> list = pageList.getRecords();

        List<String> fileUidList = new ArrayList<>();
        list.forEach(item -> {
            if (StringUtils.isNotEmpty(item.getFileUid())) {
                fileUidList.add(item.getFileUid());
            }
        });

        Map<String, String> pictureMap = fileFeignUtil.fileUidToFileUrlMap(fileUidList);
        for (UserMomentTopicEntity item : list) {
            //获取图片
            if (StringUtils.isNotEmpty(item.getFileUid()) && pictureMap.get(item.getFileUid()) != null) {
                item.setPhotoUrl(pictureMap.get(item.getFileUid()));
            }
        }
//        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<UserMomentTopicEntity> getList() {
        return null;
    }

    @Override
    public String addUserMomentTopic(UserMomentTopicDto userMomentTopicDto) {
        UserMomentTopicEntity userMomentTopic = new UserMomentTopicEntity();
        BeanUtil.copyProperties(userMomentTopicDto, userMomentTopic, SysConf.STATUS);
        userMomentTopic.insert();
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String editUserMomentTopic(UserMomentTopicDto userMomentTopicDto) {
        UserMomentTopicEntity userMomentTopic = userMomentTopicService.getById(userMomentTopicDto.getUid());
        // 插入数据【使用Spring工具类提供的深拷贝】
        BeanUtils.copyProperties(userMomentTopicDto, userMomentTopic, SysConf.STATUS, SysConf.UID);
        userMomentTopic.setUpdateTime(new Date());
        userMomentTopic.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String deleteBatchUserMomentTopic(List<UserMomentTopicDto> userMomentTopicDtoList) {
        if (userMomentTopicDtoList.size() <= 0) {
            return ResultUtil.errorWithMessage(MessageConf.PARAM_INCORRECT);
        }
        List<String> uids = new ArrayList<>();
        userMomentTopicDtoList.forEach(item -> {
            uids.add(item.getUid());
        });

        Collection<UserMomentTopicEntity> userMomentTopics = userMomentTopicService.listByIds(uids);
        userMomentTopics.forEach(item -> {
            item.setUpdateTime(new Date());
            item.setStatus(StatusEnum.DISABLED);
        });
        userMomentTopicService.updateBatchById(userMomentTopics);
        return ResultUtil.successWithMessage(MessageConf.DELETE_SUCCESS);
    }

    // endregion
    /***************************** admin:end **************************/



    /***************************** web:start **************************/
    // region didiok-web

    // endregion
    /***************************** web:end **************************/

}